home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-03-14 | 10.5 KB | 409 lines | [TEXT/MPCC] |
- /******************** ***********************/
- //
- // Player PRO 4.4x -- MTM to MADx & MADx to MTM
- //
- // Version 1.0 - 14.3.95 ANR
- //
- // To use with CodeWarrior 68K or PPC
- //
- // Antoine ROSSET
- // 16 Tranchees
- // 1206 GENEVA
- // SWITZERLAND
- //
- // FAX: (+41 22) 346 11 97
- // Compuserve: 100277,164
- // Internet: rosset@dial.eunet.ch
- //
- /******************** ***********************/
-
- #include "MTM.h"
- #include "MAD.h"
- #include "RDriver.h"
- #include "PPInOut.h"
-
- #if defined(powerc) || defined(__powerc)
- enum {
- PlayerPROPlug = kCStackBased
- | RESULT_SIZE(SIZE_CODE(sizeof(OSErr)))
- | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof( OSType)))
- | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof( Ptr*)))
- | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof( MADPartition*)))
- | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof( PPInfoRec*)))
- | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof( short**)))
- };
-
- ProcInfoType __procinfo = PlayerPROPlug;
- #else
- #include <A4Stuff.h>
- #endif
-
- unsigned long Tdecode32( void *msg_buf)
- {
- unsigned char *buf = msg_buf;
-
- return( (unsigned long) buf[3] << 24) | ( (unsigned long) buf[2] << 16) | ( (unsigned long) buf[ 1] << 8) | ( (unsigned long) buf[0]);
- }
-
- short Tdecode16( void *msg_buf)
- {
- unsigned char *buf = msg_buf;
-
- return ( (short) buf[1] << 8) | ( (short) buf[0]);
- }
-
- struct Command* GetCommand( register short PosX, register short TrackIdX, register struct MusicPattern* tempMusicPat)
- {
- if( PosX < 0) PosX = 0;
- else if( PosX >= tempMusicPat->header.PatternSize) PosX = tempMusicPat->header.PatternSize -1;
-
- return( & (tempMusicPat->Commands[ (tempMusicPat->header.PatternSize * TrackIdX) + PosX]));
- }
-
- struct MTMTrack* GetMTMCommand( short position, short whichTracks, Ptr PatPtr)
- {
- Ptr aPtr;
-
- aPtr = ( PatPtr +
- whichTracks * 192L +
- position * 3L);
-
- return (struct MTMTrack*) aPtr;
- }
-
- OSErr ConvertMTM2Mad( MTMDef *MTMFile, long MTMSize, MADPartition *theMAD, short *MADpitchTable)
- {
- short i, x, z;
- long sndSize, OffSetToSample, MPatSize, temp, inOutCount;
- Ptr MaxPtr;
- OSErr theErr;
- Ptr theInstrument[ 64], destPtr;
-
- /**** Variables pour le MAD ****/
- struct Command *aCmd;
-
- /**** Variables pour le MTM ****/
-
- struct Instru *instru[ 64];
- struct MTMTrack *theCom;
- Ptr samplePtr, patPtr, positionPtr;
- short *patTracks;
- /********************************/
-
- /**** Calcul de divers offsets *****/
-
- MTMFile->tracks = Tdecode16( &MTMFile->tracks);
- MTMFile->comments = Tdecode16( &MTMFile->comments);
-
- MaxPtr = (Ptr) ( (long) MTMFile + MTMFile);
- positionPtr = (Ptr) ( (long) MTMFile + 66L + MTMFile->NOS*37L);
- patPtr = (Ptr) ( (long) MTMFile + 194L + MTMFile->NOS*37L);
- destPtr = (Ptr) ( (long) MTMFile + 194L + MTMFile->NOS*37L + MTMFile->tracks*192L);
- patTracks = (short*) destPtr;
- samplePtr = (Ptr) ( (long) MTMFile + 194L + MTMFile->NOS*37L + MTMFile->tracks*192L +
- (MTMFile->patNo + 1L)*32L*2L + MTMFile->comments);
-
- /**** Analyse des instruments ****/
- if( MTMFile->NOS > 64) return unknowError;
-
- for( i = 0, OffSetToSample = 0L; i < MTMFile->NOS ; i++)
- {
- theInstrument[ i] = samplePtr + OffSetToSample;
-
- instru[ i] = (struct Instru*) ((long) MTMFile + 66L + i*37L);
-
- instru[ i]->size = Tdecode32( &instru[ i]->size);
- instru[ i]->loopBegin = Tdecode32( &instru[ i]->loopBegin);
- instru[ i]->loopEnd = Tdecode32( &instru[ i]->loopEnd);
-
-
- sndSize = instru[ i]->size;
- if( theInstrument[i] + sndSize > MaxPtr)
- {
- // sndSize = instru[ i]->size = MaxPtr - theInstrument[i];
- return unknowError;
- }
- OffSetToSample += sndSize;
- }
-
-
- /***********************************************/
- /******** Le MTM a été lu et analysé ***********/
- /***** Copie des informations dans le MAD ******/
- /***********************************************/
-
-
-
- /**************************/
- /*** MAD Initialisation ***/
- /**************************/
-
- inOutCount = sizeof( MADSpec);
- theMAD->header = (MADSpec*) NewPtrClear( inOutCount);
- if( theMAD->header == 0L) return needMoreMemory;
- theMAD->header->MADIdentification = 'MADG';
-
-
-
- /*****************************************/
- /*** Copie des informations MTM -> MAD ***/
- /*****************************************/
-
- for(i=0; i<22; i++)
- {
- theMAD->header->NameSignature[i] = MTMFile->songname[i];
- }
-
- theMAD->header->PatMax = MTMFile->patNo + 1;
- theMAD->header->numPointers = MTMFile->positionNo;
- for(i=0; i<128; i++)
- {
- theMAD->header->oPointers[ i] = positionPtr[ i];
- }
- theMAD->header->Tracks = MTMFile->trackback;
-
- for(i = 0; i < MTMFile->NOS; i++)
- {
- for( x = 0; x < 22; x++) theMAD->header->fid[i].Filename[x] = instru[i]->name[x];
- theMAD->header->fid[i].insSize = instru[i]->size;
- theMAD->header->fid[i].fineTune = instru[i]->fineTune;
- theMAD->header->fid[i].volume = instru[i]->volume;
- theMAD->header->fid[i].freq = 1;
- if( instru[i]->sampleSize == 0) theMAD->header->fid[i].amplitude = 8;
- else theMAD->header->fid[i].amplitude = 16;
- theMAD->header->fid[i].loopStart = instru[i]->loopBegin;
- theMAD->header->fid[i].loopLenght = instru[i]->loopEnd - instru[i]->loopBegin;
-
- if( theMAD->header->fid[ i].insSize > 0)
- {
- theMAD->instrument[ i] = NewPtr( theMAD->header->fid[i].insSize);
- if( theMAD->instrument[ i] != 0L)
- {
- BlockMove( theInstrument[i], theMAD->instrument[i], theMAD->header->fid[i].insSize);
- destPtr = theMAD->instrument[i];
- for( temp = 0; temp < theMAD->header->fid[i].insSize; temp++) *(destPtr + temp) -= 0x80;
- }
- else return needMoreMemory;
- }
- else theMAD->instrument[i] = 0L;
- }
-
- for( i = MTMFile->NOS; i < MAXINSTRU ; i++)
- {
- theMAD->instrument[i] = 0L;
- theMAD->header->fid[i].volume = 64;
- theMAD->header->fid[i].freq = 1;
- theMAD->header->fid[i].amplitude = 8;
- }
-
-
- for(i=0; i<theMAD->header->PatMax; i++)
- {
-
- theMAD->partition[ i] = (struct MusicPattern*) NewPtrClear( sizeof( struct MusicPattern) + theMAD->header->Tracks * 64L * sizeof( struct Command));
- if( theMAD->partition[ i] == 0L) return needMoreMemory;
-
- theMAD->partition[ i]->header.PatternSize = 64L;
- theMAD->partition[ i]->header.CompressionMode = 'NONE';
-
- for( x = 0; x < 20; x++) theMAD->partition[ i]->header.PatternName[ x] = 0;
-
- theMAD->partition[ i]->header.PatBytes = 0L;
- theMAD->partition[ i]->header.unused2 = 0L;
-
- MaxPtr = (Ptr) theMAD->partition[ i];
- MaxPtr += sizeof( struct MusicPattern) + theMAD->header->Tracks * 64L * sizeof( struct Command);
-
- for( z = 0; z < 32; z++) patTracks[ z] = Tdecode16( &patTracks[ z]);
-
- for(x=0; x<64; x++)
- {
- for(z=0; z<theMAD->header->Tracks; z++)
- {
- aCmd = GetCommand( x, z, theMAD->partition[ i]);
- if( (Ptr) aCmd + sizeof( struct Command) > MaxPtr) return unknowError;
-
- if( patTracks[ z] == 0)
- {
- aCmd->InstrumentNo = 0;
- aCmd->AmigaPeriod = 0;
- aCmd->EffectCmd = 0;
- aCmd->EffectArg = 0;
- }
- else
- {
- theCom = GetMTMCommand( x,
- patTracks[ z],
- (Ptr) patPtr);
-
- aCmd->InstrumentNo = theCom->instru;
- aCmd->AmigaPeriod = theCom->pitch;
-
- aCmd->EffectCmd = theCom->EffectCmd;
- aCmd->EffectArg = theCom->EffectArg;
- }
- }
- }
-
- /*** Avance dans les tracks-patterns suivants ***/
- patTracks += 32;
- }
-
- return noErr;
- }
-
- OSErr ExtractInfo( PPInfoRec *info, MTMDef *myFile)
- {
- long PatternSize;
- short i;
- short maxInstru;
- short tracksNo;
-
- for( i = 0; i < sizeof( myFile->songname); i++)
- {
- info->internalFileName[ i] = myFile->songname[ i];
- }
- info->internalFileName[ 21] = 0;
- CtoPstr( (Ptr) info->internalFileName);
-
- pStrcpy( info->formatDescription, "\pMTM Plug");
-
- info->totalPatterns = myFile->patNo;
- info->partitionLength = myFile->positionNo;
- info->totalTracks = myFile->tracks;
- info->totalInstruments = myFile->NOS;
- info->signature = 'MTM ';
-
- return noErr;
- }
-
- OSErr TestFile( MTMDef *myFile)
- {
- if( myFile->Id[ 0] == 'M' &&
- myFile->Id[ 1] == 'T' &&
- myFile->Id[ 2] == 'M') return noErr;
-
- else return fileNotSupportedByThisPlug;
- }
-
- void pStrcpy(register unsigned char *s1, register unsigned char *s2)
- {
- register short len, i;
-
- len = *s2;
- for ( i = 0; i <= len; i++) s1[ i] = s2[ i];
- }
-
- /*****************/
- /* MAIN FUNCTION */
- /*****************/
-
- OSErr main( OSType order, FSSpec *AlienFileFSSpec, MADPartition *MadFile, PPInfoRec *info, short *MADpitchTable)
- {
- OSErr myErr;
- Ptr AlienFile;
- short vRefNum, iFileRefI;
- long dirID, sndSize;
-
- #ifndef powerc
- long oldA4 = SetCurrentA4(); //this call is necessary for strings in 68k code resources
- #endif
-
- HGetVol( 0L, &vRefNum, &dirID);
- HSetVol( 0L, AlienFileFSSpec->vRefNum, AlienFileFSSpec->parID);
-
- myErr = noErr;
-
- switch( order)
- {
- case 'IMPL':
- myErr = FSOpen( AlienFileFSSpec->name, 0, &iFileRefI);
- if( myErr == noErr)
- {
- GetEOF( iFileRefI, &sndSize);
-
- // ** TEST MEMOIRE : Environ 2 fois la taille du fichier**
- AlienFile = NewPtr( sndSize * 2L);
- if( AlienFile == 0L) myErr = needMoreMemory;
- // **
-
- else
- {
- DisposPtr( AlienFile);
-
- AlienFile = NewPtr( sndSize);
- myErr = FSRead( iFileRefI, &sndSize, AlienFile);
- if( myErr == noErr)
- {
- myErr = TestFile( (MTMDef*) AlienFile);
- if( myErr == noErr)
- {
- myErr = ConvertMTM2Mad( (MTMDef*) AlienFile, GetPtrSize( AlienFile), MadFile, MADpitchTable);
- }
- }
- DisposPtr( AlienFile); AlienFile = 0L;
- }
- FSClose( iFileRefI);
- }
- break;
-
- case 'TEST':
- myErr = FSOpen( AlienFileFSSpec->name, 0, &iFileRefI);
- if( myErr == noErr)
- {
- sndSize = 5000L; // Read only 5000 first bytes for optimisation
-
- AlienFile = NewPtr( sndSize);
- if( AlienFile == 0L) myErr = needMoreMemory;
- else
- {
- myErr = FSRead( iFileRefI, &sndSize, AlienFile);
- if( myErr == noErr)
- {
- myErr = TestFile( (MTMDef*) AlienFile);
- }
- DisposPtr( AlienFile); AlienFile = 0L;
- }
- FSClose( iFileRefI);
- }
- break;
-
- case 'EXPL':
- myErr = orderNotImplemented;
- break;
-
- case 'INFO':
- myErr = FSOpen( AlienFileFSSpec->name, 0, &iFileRefI);
- if( myErr == noErr)
- {
- GetEOF( iFileRefI, &info->fileSize);
-
- sndSize = 5000L; // Read only 5000 first bytes for optimisation
-
- AlienFile = NewPtr( sndSize);
- if( AlienFile == 0L) myErr = needMoreMemory;
- else
- {
- myErr = FSRead( iFileRefI, &sndSize, AlienFile);
- if( myErr == noErr)
- {
- myErr = ExtractInfo( info, (MTMDef*) AlienFile);
- }
- DisposPtr( AlienFile); AlienFile = 0L;
- }
- FSClose( iFileRefI);
- }
- break;
-
- default:
- myErr = orderNotImplemented;
- break;
- }
-
- HSetVol( 0L, vRefNum, dirID);
-
- #ifndef powerc
- SetA4( oldA4);
- #endif
- return myErr;
- }